<template>
  <div>
    <!--条件筛选-->
    <TopFilter ref="topFilter" :list="topFilterList" :cols="3" labelWidth="110" @filterChange="changeHandle"
               @onCollapse="collapseHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams2"
      :isSelectColumn="false"
      datakey="records"
      isExportExcel2
      :onRowSelectChange="selectChange"
      :onColumnsChange="columns => this.columns = columns"
    >
      <template slot="controls" slot-scope="props">
      </template>
    </FilterTable>
    <Drawer :visible.sync="visibleDrawer" :title="dialogTitle" destroyOnClose width="80vw"
            :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <PartOrderDetail :handleType="handleType" :pageType="pageType" :value="formData" @close="closeHandler"/>
    </Drawer>
    <BaseDialog :visible.sync="visibleForm" width="500px" title="审批驳回" destroyOnClose>
      <FormPanel
        ref="baseInfo"
        :list="initFormBase"
        :cols="1"
        :labelWidth="140"
        formType="add"
        isSubmitBtn
        @formChange="sub"
      />
    </BaseDialog>
  </div>
</template>

<script>
import {getSpecialPartOrder, postUpdateStatus} from '@/api/partsmanage/purchaseManage/purchaseSubmit';
import {ddlxArr, ddztArr} from './constant';
import PartOrderDetail from './partOrderDetail';
import {dictionary} from '@/utils/dictMixin';
import { getCurrentUser } from '@/api/cms-api/origin/construct/dealer';
export default {
  name: "purchaseSubmitApproval",
  components: {
    PartOrderDetail
  },
  mixins: [dictionary],
  data() {
    return {
      topFilterList: this.createTopFilterList(),
      columns: this.createTableColumns(),
      fetchApi: getSpecialPartOrder,
      pageType: 'OEM3', //特殊追溯订单审批
      fetchParams: {
        xhrAbort: true
      },
      visibleDrawer: false,
      visibleForm: false,
      handleType: '',
      formData: {},
      row: {},
      dialogTitle: '配件采购明细',
      rowSelectData: [],
      initFormBase: this.initFormBaseFun(),
    };
  },
  computed: {
    fetchParams2() {
      return {
        pageType: this.pageType,
        ...this.fetchParams
      }
    }
  },
  async created() {
    this.columns.find(x => x.dataIndex === 'xsddsx').hidden = true;
    this.columns.find(x => x.dataIndex === 'xsddsx').dictItems = this.createDictList('5000');
    // 获取当前登录人信息
    await getCurrentUser({}).then(response => {
      console.info(response);
      if (response.data.appId === 'beijing') {
        console.info(this.topFilterList);
        this.topFilterList.find(x => x.fieldName === 'xsddsx').hidden = false;
        this.columns.find(x => x.dataIndex === 'xsddsx').hidden = false;
      }
    });
  },
  methods: {
    createTopFilterList() {
      return [
        {type: 'INPUT', label: '经销商代码', fieldName: 'dealerCodeQ', placeholder: '请输入'},
        {type: 'INPUT', label: '经销商名称', fieldName: 'dealerNameQ', placeholder: '请输入'},
        {type: 'INPUT', label: '销售单号', fieldName: 'xsddhm', placeholder: '请输入'},
        {type: 'SELECT', label: '订单类型', fieldName: 'xsddlx', placeholder: '请选择', itemList: ddlxArr},
        {type: 'SELECT', label: '订单属性', fieldName: 'xsddsx', placeholder: '请选择', hidden: true, itemList: this.createDictList('5000')},
        {type: 'INPUT', label: '配件代码', fieldName: 'bjxmbh', placeholder: '请输入配件代码'},
        {type: 'INPUT', label: '配件名称', fieldName: 'bjzwmc', placeholder: '请输入配件名称'},
      ]
    },
    createTableColumns() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 160,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.detailHandle(props.row, 'show')}>
                  明细
                </el-button>
                <el-divider direction="vertical"/>
                <el-button size="mini" type="text" onClick={() => this.handleZt(props.row, 'TS')}>
                  审批通过
                </el-button>
                <el-divider direction="vertical"/>
                <el-button size="mini" type="text" onClick={() => {
                  this.row = props.row;
                  this.visibleForm = true;
                }}>
                  驳回
                </el-button>
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
        },
        {
          title: '销售单号',
          dataIndex: 'xsddhm',
          width: 150,
          sorter: true,
        },
        {
          title: 'SAP单号',
          dataIndex: 'sapdh',
          sorter: true,
        },
        {
          title: '订单类型',
          dataIndex: 'xsddlx',
          width: 150,
          sorter: true,
          dictItems: ddlxArr,
        },
        {
          title: '订单属性',
          dataIndex: 'xsddsx',
          width: 150,
          sorter: true,
          dictItems: this.createDictList(5000),
        },
        {
          title: '订单状态',
          dataIndex: 'xsddzt',
          width: 150,
          sorter: true,
          dictItems: ddztArr,
        },
        {
          title: '配件代码',
          dataIndex: 'ownerCode',
          sorter: true,
        },
        {
          title: '配件名称',
          dataIndex: 'ownerParCode',
          sorter: true,
        },
        {
          title: '项目数量',
          dataIndex: 'bszpzs',
          sorter: true,
        },
        {
          title: '采购数量',
          dataIndex: 'bszsls',
          sorter: true,
        },
        {
          title: '采购总金额',
          dataIndex: 'xsysje',
          numberFormat: true,
          sorter: true,
        },
        {
          title: '创建日期',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
      ]
    },
    //基本表单信息
    initFormBaseFun() {
      return [
        {
          type: 'TEXT_AREA',
          label: '驳回原因',
          fieldName: 'ddbhbz',
          placeholder: '请输入',
          initialValue: '',
          rules: [
            {required: true, trigger: 'change', message: '请填写驳回原因'},
          ],
        },
      ];
    },
    addHandle() {
      this.handleType = 'add';
      this.visibleDrawer = true
    },
    detailHandle(row, handleType) {
      this.handleType = handleType;
      this.formData = {
        id: row.id,
        data: {
          ...row,
          details: []
        }
      };
      this.visibleDrawer = true
    },
    /**
     * 表单提交
     */
    async sub(data) {
      console.info(data)
      const res = await postUpdateStatus({
        id: this.row.id,
        xsddzt: 'TB',
        ...data
      })
      if (res.resultCode == 200) {
        if (res.data) {
          this.$notify.warning({title: '提示信息', message: res.data});
        } else {
          this.visibleForm = false;
          this.$notify.success({title: '提示信息', message: '驳回成功'});
          // 重新加载
          this.fetchParams = Object.assign({}, this.fetchParams);
        }
      }
    },
    handleZt(row, xsddzt) {
      this.$confirm(`您确定要${xsddzt == 'TS' ? '审批通过' : '审批驳回'}吗？`, '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await postUpdateStatus({
          id: row.id,
          xsddzt
        })
        if (res.resultCode == 200) {
          if (res.data) {
            this.$notify.warning({title: '提示信息', message: res.data});
          } else {
            this.$notify.success({title: '提示信息', message: '操作成功！'});
            // 重新加载
            this.fetchParams = Object.assign({}, this.fetchParams);
          }
        }
        console.log('确定')
      }).catch(() => {
        console.log('取消')
      });
    },
    closeHandler(type) {
      this.visibleDrawer = false
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    selectChange(val) {
      console.log('val', val);
      this.rowSelectData = val;
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
  }
}
</script>

<style scoped>

</style>
